package @@packageName@@;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.google.gwt.json.client.*;

public class MapJsonizer {
    public static Map<String, Character> toJavaChar(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Character>() {
            public Character toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaChar(arg0);
            }
        });
    }

    public static Map<String, String> toJavaString(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<String>() {
            public String toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaString(arg0);
            }
        });
    }

    public static Map<String, Boolean> toJavaBoolean(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Boolean>() {
            public Boolean toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaBoolean(arg0);
            }
        });
    }

    public static Map<String, Short> toJavaShort(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Short>() {
            public Short toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaShort(arg0);
            }
        });
    }

    public static Map<String, Integer> toJavaInteger(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Integer>() {
            public Integer toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaInteger(arg0);
            }
        });
    }

    public static Map<String, Long> toJavaLong(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Long>() {
            public Long toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaLong(arg0);
            }
        });
    }

    public static Map<String, Float> toJavaFloat(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Float>() {
            public Float toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaFloat(arg0);
            }
        });
    }

    public static Map<String, Double> toJavaDouble(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Double>() {
            public Double toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaDouble(arg0);
            }
        });
    }

    public static Map<String, Date> toJavaDate(JSONValue arg0) {
        return toJava(arg0, new JavaConverter<Date>() {
            public Date toJava(JSONValue arg0) {
                return ObjectJsonizer.toJavaDate(arg0);
            }
        });
    }

    public static JSONValue toJsonChar(Map<String, Character> arg0) {
        return toJson(arg0, new JsonConverter<Character>() {
            public JSONValue toJson(Character arg0) {
                return ObjectJsonizer.toJsonChar(arg0);
            }
        });
    }

    public static JSONValue toJsonString(Map<String, String> arg0) {
        return toJson(arg0, new JsonConverter<String>() {
            public JSONValue toJson(String arg0) {
                return ObjectJsonizer.toJsonString(arg0);
            }
        });
    }

    public static JSONValue toJsonBoolean(Map<String, Boolean> arg0) {
        return toJson(arg0, new JsonConverter<Boolean>() {
            public JSONValue toJson(Boolean arg0) {
                return ObjectJsonizer.toJsonBoolean(arg0);
            }
        });
    }

    public static JSONValue toJsonShort(Map<String, Short> arg0) {
        return toJson(arg0, new JsonConverter<Short>() {
            public JSONValue toJson(Short arg0) {
                return ObjectJsonizer.toJsonShort(arg0);
            }
        });
    }

    public static JSONValue toJsonInteger(Map<String, Integer> arg0) {
        return toJson(arg0, new JsonConverter<Integer>() {
            public JSONValue toJson(Integer arg0) {
                return ObjectJsonizer.toJsonInteger(arg0);
            }
        });
    }

    public static JSONValue toJsonLong(Map<String, Long> arg0) {
        return toJson(arg0, new JsonConverter<Long>() {
            public JSONValue toJson(Long arg0) {
                return ObjectJsonizer.toJsonLong(arg0);
            }
        });
    }

    public static JSONValue toJsonFloat(Map<String, Float> arg0) {
        return toJson(arg0, new JsonConverter<Float>() {
            public JSONValue toJson(Float arg0) {
                return ObjectJsonizer.toJsonFloat(arg0);
            }
        });
    }

    public static JSONValue toJsonDouble(Map<String, Double> arg0) {
        return toJson(arg0, new JsonConverter<Double>() {
            public JSONValue toJson(Double arg0) {
                return ObjectJsonizer.toJsonDouble(arg0);
            }
        });
    }

    public static JSONValue toJsonDate(Map<String, Date> arg0) {
        return toJson(arg0, new JsonConverter<Date>() {
            public JSONValue toJson(Date arg0) {
                return ObjectJsonizer.toJsonDate((Date) arg0);
            }
        });
    }

    private static <T> Map<String, T> toJava(JSONValue arg0, JavaConverter<T> converter) {
        if (arg0.isNull() != null) {
            return null;
        }

        JSONObject jsonObject = arg0.isObject();
        if (jsonObject == null) {
            throw new JSONException("expected JSONNull or JSONObject");
        }

        Map<String, T> map = new HashMap<String, T>();

        for (String key : jsonObject.keySet()) {
            map.put(key, converter.toJava(jsonObject.get(key)));
        }

        return map;
    }

    private static <T> JSONValue toJson(Map<String, T> arg0, JsonConverter<T> converter) {
        if (arg0 == null) {
            return JSONNull.getInstance();
        }

        JSONObject jsonObject = new JSONObject();

        for (String key : arg0.keySet()) {
            jsonObject.put(key, converter.toJson(arg0.get(key)));
        }

        return jsonObject;
    }

    private interface JavaConverter<T> {
        public T toJava(JSONValue arg0);
    }

    private interface JsonConverter<T> {
        public JSONValue toJson(T arg0);
    }
}
